﻿using NPOI.HSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace DataServer.Utils
{
    public class ToDataUtils
    {
        public static DataTable GetDataTableByType(string filepath, string filetype)
        {
            if (filetype.ToUpper().Contains("XLS"))
            {
                return ReadExcel(filepath);
            }
            else if(filetype.ToUpper().Contains("CSV"))
            {
                return GetdataFromCVS(filepath);
            }
            else if (filetype.ToUpper().Contains("TXT"))
            {
                return GetDataFromTXT(filepath);
            }
            else
            {
                throw new Exception("文件类型不支持!");
            }
        }

        #region NPOI读取Excel文件
        /// <summary>  
        /// 读取Excel文件到table中  
        /// </summary>  
        /// <param name="filePath">excel文件路径</param>  
        /// <returns></returns>  
        private static DataTable ReadExcel(string filePath)
        {
            DataTable dt = new DataTable();
            dt = ImportExcelFile(filePath);

            //文件是否存在  
            if (System.IO.File.Exists(filePath))
            {

            }
            return dt;
        }
        private static DataTable ImportExcelFile(string filePath)
        {
            HSSFWorkbook hssfworkbook;
            #region//初始化信息
            try
            {
                using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    hssfworkbook = new HSSFWorkbook(file);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            #endregion

            NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
            DataTable dt = new DataTable();
            rows.MoveNext();
            HSSFRow row = (HSSFRow)rows.Current;
            for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
            {
                //dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());  
                //将第一列作为列表头  
                dt.Columns.Add(row.GetCell(j).ToString());
            }
            while (rows.MoveNext())
            {
                row = (HSSFRow)rows.Current;
                DataRow dr = dt.NewRow();
                for (int i = 0; i < row.LastCellNum; i++)
                {
                    NPOI.SS.UserModel.ICell cell = row.GetCell(i);
                    if (cell == null)
                    {
                        dr[i] = null;
                    }
                    else
                    {
                        dr[i] = cell.ToString();
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        #endregion    

        #region 读取CSV文件
        private static DataTable GetdataFromCVS(string filepath)
        {
            DataTable dt = new DataTable();
            StreamReader sr = new StreamReader(filepath,System.Text.Encoding.GetEncoding(936));
            string strTitle = sr.ReadLine();
            string[] strColumTitle = strTitle.Split(',');   //CVS 文件默认以逗号隔开
            for (int i = 0; i < strColumTitle.Length; i++)
            {
                dt.Columns.Add(strColumTitle[i]);
            }
            while (!sr.EndOfStream)
            {
                string strTest = sr.ReadLine();
                string[] strTestAttribute = strTest.Split(',');
                DataRow dr = dt.NewRow();
                for (int i = 0; i < strColumTitle.Length; i++)
                {
                    dr[strColumTitle[i]] = strTestAttribute[i];
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        #endregion

        #region 读取txt
        private static DataTable GetDataFromTXT(string filepath)
        {
            return GetdataFromCVS(filepath);
        }
        #endregion
    }
}
